package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 添加好友关系
     *   POST    /contacts
     *   请求参数：userId (好友的id)
     */
    @PostMapping("/contacts")
    public ResponseEntity contacts(@RequestBody Map map) {
        //提取参数
        String str = map.get("userId").toString();
        Long friendId = Long.valueOf(str);
        messagesService.contacts(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询好友列表
     */
    @GetMapping("/contacts")
    public ResponseEntity contacts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult vo = messagesService.findFriends(page,pagesize);
        return ResponseEntity.ok(vo);
    }

    /**
     * 根据环信的id，查询用户信息
     *    /userinfo
     *   参数：huanxinId  : hx106
     *   返回值：userInfovo
     */
    @GetMapping("/userinfo")
    public ResponseEntity userinfo(String huanxinId) {
        UserInfoVo vo = messagesService.findUserInfo(huanxinId);
        return ResponseEntity.ok(vo);
    }
}
